require 'test/unit'
require 'lib/smppclient/pdu/pdu'
require 'lib/smppclient/pdu/command_id'
require_relative 'sample_pdu'

class TestPdu < MiniTest::Unit::TestCase

  include SmppClient::Pdu
  include SmppClient::Util

  def test_empty_init
    pdu = SamplePdu.new
    assert_equal(Pdu::HEADER_LENGTH, pdu.command_length)
    assert_equal(SamplePdu::COMMAND_ID, pdu.command_id)
    assert_equal(CommandStatus::ESME_ROK, pdu.command_status)
    assert_equal(0, pdu.sequence_number)
  end

  def test_byte_buffer_init
    bb = ByteBuffer.new
    bb.append_int(41)
    bb.append_int(9)
    bb.append_int(0)
    bb.append_int(1)
    bb.append_cstring('bulat')
    bb.append_cstring('java')
    bb.append_cstring('Logica')
    bb.append_byte(0x34)
    bb.append_byte(0x01)
    bb.append_byte(0x01)
    bb.append_cstring('11*')

    pdu = SamplePdu.new(bb)
    assert_equal(41, pdu.command_length)
    assert_equal(CommandId::BIND_TRANSCEIVER, pdu.command_id)
    assert_equal(CommandStatus::ESME_ROK, pdu.command_status)
    assert_equal(1, pdu.sequence_number)
    assert_equal('00000010000000090000000000000001', pdu.buffer.to_s)
  end

  def test_parse_header_length_error
    bb = ByteBuffer.new
    bb.append_int(8)
    bb.append_int(9)
    assert_raises(SmppClient::Pdu::PduError) { SamplePdu.new(bb) }
  end

  def test_parse_pdu_length_error
    bb = ByteBuffer.new
    bb.append_int(100)
    bb.append_int(9)
    bb.append_int(0)
    bb.append_int(1)
    assert_raises(SmppClient::Pdu::PduError) { SamplePdu.new(bb) }
  end

end